package com.aishare.qicaitaoke.ui.fragment;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aishare.qicaitaoke.R;
import com.aishare.qicaitaoke.adapter.ImgAdapter;
import com.aishare.qicaitaoke.adapter.LoopVPAdapter;
import com.aishare.qicaitaoke.ui.view.AutoScrollViewPager;
import com.aishare.qicaitaoke.ui.view.CircleIndicator;
import com.aishare.qicaitaoke.ui.view.ObservableScrollView;
import com.aishare.qicaitaoke.utils.KLog;
import com.aishare.qicaitaoke.utils.StatusBarUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexFragment extends BaseFragment {

    private AutoScrollViewPager viewPager;
    private ArrayList<String> urls = new ArrayList<>();
    private LoopVPAdapter loopVPAdapter;
    private CircleIndicator circleIndicator;
    private ObservableScrollView scrollView;

    private RefreshLayout mRefreshLayout;
    private View topLayout,barLayout;
    private ClassicsHeader mClassicsHeader;
    private Drawable mDrawableProgress;

    public IndexFragment() {
        // Required empty public constructor
    }

    @Override
    public int getLayoutResources() {
        return R.layout.index_layout;
    }

    @Override
    void initView() {
        urls.add("http://seopic.699pic.com/photo/00005/5186.jpg_wh1200.jpg");
        urls.add("http://seopic.699pic.com/photo/50010/0719.jpg_wh1200.jpg");
        urls.add("http://seopic.699pic.com/photo/50009/9449.jpg_wh1200.jpg");
        urls.add("http://seopic.699pic.com/photo/50002/5923.jpg_wh1200.jpg");
        urls.add("http://seopic.699pic.com/photo/50001/9330.jpg_wh1200.jpg");
        urls.add("http://seopic.699pic.com/photo/50009/9191.jpg_wh1200.jpg");
        viewPager = rootView.findViewById(R.id.index_pager);
        circleIndicator = rootView.findViewById(R.id.indicator);
        topLayout = rootView.findViewById(R.id.top_layout);
        barLayout = rootView.findViewById(R.id.bar_layout);
        scrollView = rootView.findViewById(R.id.index_scroll);
        //状态栏透明和间距处理
        StatusBarUtil.immersive(getActivity(), Color.WHITE, 0.0f);
        StatusBarUtil.setPaddingSmart(getActivity(), topLayout);
        loopVPAdapter = new ImgAdapter(getActivity(), urls, viewPager);
        viewPager.startAutoScroll(1500);
        circleIndicator.setViewPager(viewPager);
        mRefreshLayout = rootView.findViewById(R.id.index_refresh_layout);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1000);
                viewPager.startAutoScroll(1500);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore();
                viewPager.startAutoScroll(1500);
            }
        });

        scrollView.setOnScollChangedListener(new ObservableScrollView.OnScollChangedListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                LinearLayout linearLayout = (LinearLayout) scrollView.getChildAt(0);
                int height = linearLayout.getChildAt(0).getHeight();
                float alpha =((float) y)/((float) height);
                KLog.e("height = "+height + " y ="+y +" oldy = "+oldy + " alpha = "+alpha);
                StatusBarUtil.immersive(getActivity(),getResources().getColor(R.color.white),alpha);
                barLayout.setAlpha(alpha);
            }
        });

    }



}
